home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9563 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  54 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: malloc question
  5. Message-ID: <1996Mar11.184238.6959@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de>
  11. Date: Mon, 11 Mar 1996 18:42:03 GMT
  12.  
  13. watzka@stat.uni-muenchen.de (Kurt Watzka) wrote:
  14.  
  15. >alex@station.net (Alex Chu) writes:
  16.  
  17. >>Hi everybody,
  18.  
  19. >>I have a question for the following snip C program.
  20.  
  21. >>typedef struct item {
  22. >>  int val;
  23. >>  struct item *next;
  24. >>} ITEM, *PITEM;
  25.  
  26. >>main()
  27. >>{
  28. >>  PITEM head, current;
  29. >>  head=(PITEM) malloc(sizeof(ITEM));
  30. >>            ^^^^^^^
  31. >>  head->val=1;
  32. >>}
  33.  
  34. >>I want to know why need to use the type casting PITEM in front of the
  35. >>malloc ?  Please help!
  36.  
  37. >Simple answer: You don't have to use that cast, and you should _not_ use
  38. >it, because all you can do with that cast is _hide_ an error.
  39.  
  40. True.  The example was missing #include <stdlib.h> which should have
  41. quieted his compiler about converting the return type of malloc
  42. (assumed int) to a pointer.
  43.  
  44. >OTOH, if you are using a C++ compiler to translate C programs, the cast
  45. >is needed, but malloc() is obsolete.
  46.  
  47. Mostly, not entirely.  If you want to use realloc, you need to use
  48. malloc/free instead of new/delete in C++.
  49.  
  50. --
  51. Richard Krehbiel, Kastle Systems, Arlington VA USA
  52. rich@kastle.com (work) or richk@mnsinc.com (personal)
  53.  
  54.